package com.wxzz.elearing.user.api.app;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.config.annotation.NacosValue;
import com.wxzz.elearing.user.dto.BingPhoneDto;
import com.wxzz.elearing.user.dto.UpdateWxUserDto;
import com.wxzz.elearing.user.dto.WxLoginDto;
import com.wxzz.elearing.user.entity.Enterprise;
import com.wxzz.elearing.user.entity.WxUser;
import com.wxzz.elearing.user.service.IWxUserService;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;


@RestController
@RequestMapping("/wx-user")
@Api(tags = "微信用户接口")
public class WxUserController {

    @Autowired
    private IWxUserService wxUserService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @ApiOperation(value = "微信授权登录", notes = "微信授权登录", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "wxLogin", method = RequestMethod.POST)
    public ReturnVO<Map> wxLogin(@RequestBody WxLoginDto wxLoginDto) throws Exception {
        return wxUserService.wxLogin(wxLoginDto);
    }

    @ApiOperation(value = "授权登录后绑定手机号", notes = "授权登录后绑定手机号", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "bingPhone", method = RequestMethod.POST)
    public ReturnVO bingPhone(@RequestHeader String token, @RequestBody BingPhoneDto bingPhoneDto) throws Exception {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return wxUserService.bindPhone(jsonObject.getLong("id"), bingPhoneDto);
    }

    @ApiOperation(value = "用户修改信息", notes = "用户修改信息", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "updateWxUser", method = RequestMethod.POST)
    public ReturnVO updateWxUser(@RequestHeader String token, @RequestBody UpdateWxUserDto updateWxUserDto) throws Exception {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        updateWxUserDto.setId(jsonObject.getLong("id"));
        return wxUserService.updateWxUser(updateWxUserDto);
    }

    @ApiOperation(value = "发送验证码", notes = "发送验证码", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "sendCode/{phone}", method = RequestMethod.POST)
    public ReturnVO sendCode(@PathVariable String phone) {
        return wxUserService.sendCode(phone);
    }


    @ApiOperation(value = "访问首页时调用", notes = "访问首页时调用", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "saveVisitNum/{enterpriseId}", method = RequestMethod.POST)
    public ReturnVO saveVisitNum(@PathVariable Long enterpriseId) {
        return wxUserService.saveVisitNum(enterpriseId);
    }

    @ApiOperation(value = "token换取用户信息", notes = "token换取用户信息", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "getWxUser", method = RequestMethod.POST)
    public ReturnVO<WxUser> getWxUser(@RequestHeader String token) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return wxUserService.getWxUser(jsonObject.getLong("id"));
    }

    @ApiOperation(value = "token换取用户企业信息", notes = "token换取用户企业信息", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "getEnterprise", method = RequestMethod.POST)
    public ReturnVO<Enterprise> getEnterprise(@RequestHeader String token) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return wxUserService.getEnterprise(jsonObject.getLong("id"));
    }

}

